package com.systemic.controller;

import com.systemic.core.ResponseResult;
import com.systemic.pojo.MindfulnessClass;
import com.systemic.service.MindfulnessClassService;
import com.systemic.vo.PageVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: MindfulnessClassController
 * @Description:
 * @Author:
 * @CreateTime: 2024-01-26 10:00
 * @Version:
 **/
@RestController
@RequestMapping("/mindfulnessClass")
public class MindfulnessClassController {

    @Resource
    private MindfulnessClassService service;

    @PostMapping("/add")
    public ResponseResult<String> add(@RequestParam("image") MultipartFile image,
                                      @RequestParam("className") String className,
                                      @RequestParam("classInfo") String classInfo,
                                      @RequestParam("author") Integer author){

        MindfulnessClass mindfulnessClass = new MindfulnessClass();
        mindfulnessClass.setClassName(className);
        mindfulnessClass.setClassInfo(classInfo);
        mindfulnessClass.setAuthor(author);
        int row = service.add(image, mindfulnessClass);
        return row!=0 ? ResponseResult.success("新增成功") : ResponseResult.failed("新增失败");
    }

    @GetMapping("/delete/{id}")
    public ResponseResult<String> delete(@PathVariable("id") Integer id){
        int row = service.delete(id);
        return row!=0 ? ResponseResult.success("删除成功") : ResponseResult.failed("删除失败");
    }

    @GetMapping("/deleteList/{ids}")
    public ResponseResult<String> deleteList(@PathVariable("ids") Integer[] ids){
        int row = service.deleteList(ids);
        return row!=0 ? ResponseResult.success("删除成功") : ResponseResult.failed("删除失败");
    }

    @PostMapping("/update")
    public ResponseResult<String> update(@RequestParam("image") MultipartFile image,
                                         @RequestParam("className") String className,
                                         @RequestParam("classInfo") String classInfo,
                                         @RequestParam("id") Integer id){

        MindfulnessClass mindfulnessClass = new MindfulnessClass();
        mindfulnessClass.setId(id);
        mindfulnessClass.setClassName(className);
        mindfulnessClass.setClassInfo(classInfo);
        int row = service.update(image, mindfulnessClass);
        return row!=0 ? ResponseResult.success("修改成功") : ResponseResult.failed("修改失败");
    }

    @GetMapping("/selectById/{id}")
    public ResponseResult<MindfulnessClass> selectById(@PathVariable("id") Integer id){
        MindfulnessClass res = service.selectById(id);
        return res!=null ? ResponseResult.success(res) : ResponseResult.failed();
    }

    @PostMapping("/likeSelect")
    public ResponseResult<List<MindfulnessClass>> likeSelect(@RequestBody MindfulnessClass mindfulnessClass){
        List<MindfulnessClass> res = service.likeSelect(mindfulnessClass.getClassName());
        return !res.isEmpty() ? ResponseResult.success(res) : ResponseResult.failed();
    }

    @PostMapping("/likeSelectPage")
    public ResponseResult<List<MindfulnessClass>> likeSelectPage(@RequestBody PageVo pageVo){
        List<MindfulnessClass> res = service.likeSelectPage(pageVo);
        return !res.isEmpty() ? ResponseResult.success(res) : ResponseResult.failed();
    }


}
